<?php
/**
 * Lookup Model Class File
 *
 * @package
 * @author
 */

/**
 * This is the model class for table "{{lookup}}".
 *
 * The followings are the available columns in table '{{lookup}}':
 * @property integer $id
 * @property string $name
 * @property integer $code
 * @property string $type
 * @property integer $position
 *
 * @package
 * @author
 */
class Lookup extends MyModel
{
    // {{{ *** Members ***

    /**
     * List of fields composing the record name (separated by ;)
     * @var string
     */
    var $nameFields = "type;position;name;code";

    /**
     * Name format string (e.g. '{1} (#{2})' 
     * @var string
     */
    var $nameFormat = "{1} - {2}) {3} ({4})";
    
    /**
     * Does this model use record revisions?
     * @var bool
     */
    var $usesRevision   = false;

    /**
     * Revision number of current record
     * @var int
     */
    var $recordRevision;

    /**
     * Does this model use logging?
     * @var bool
     */
    var $usesLog        = false;

    private static $_items;

    // }}} 
    // {{{ *** Methods ***
    // {{{ model
    /**
     * Returns the static model of the specified AR class.
     * @return Lookup the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    } // }}} 
    // {{{ tableName
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{Lookup}}';
    } // }}} 
    // {{{ rules
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, code, type, position', 'required'),
            array('position', 'numerical', 'integerOnly'=>true),
            array('type, code, name', 'length', 'max'=>255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, code, type, position', 'safe', 'on'=>'search'),
        );
    } // }}} 
    // {{{ relations
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    } // }}} 
    // {{{ attributeLabels
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => _('ID'),
            'name' => _('Name'),
            'code' => _('Code'),
            'type' => _('Type'),
            'position' => _('Position'),
        );
    } // }}} 
    // {{{ search
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('type',$this->type,true);
        $criteria->compare('position',$this->position);
        $criteria->compare('code',$this->code, true);
        $criteria->compare('name',$this->name,true);

        $criteria->order = 'type,position,name';
        return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria));
    } // }}} 
    // {{{ items
    /**
     * Returns the items for the specified type.
     * @param string item type (e.g. 'PostStatus').
     * @return array item names indexed by item code. The items are order by their position values.
     * An empty array is returned if the item type does not exist.
     */
    public static function items($type=null)
    {
            if(!isset(self::$_items[$type]))
                self::loadItems($type);
            return self::$_items[$type];
    } // }}} 
    // {{{ item
    /**
     * Returns the item name for the specified type and code.
     * @param string the item type (e.g. 'PostStatus').
     * @param integer the item code (corresponding to the 'code' column value)
     * @return string the item name for the specified the code. False is returned if the item type or code does not exist.
     */
    public static function item($type,$code)
    {
        if(!isset(self::$_items[$type]))
            self::loadItems($type);
        return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;
    } // }}}
    public function getAllTypes()
    {
        $items=array();
        $models=self::model()->findAll(array(
            'order'=>'type',
        ));
        foreach($models as $model)
            $items[$model->type]=$model->type;
        return $items;
    }
    // {{{ loadItems
    /**
     * Loads the lookup items for the specified type from the database.
     * @param string the item type
     */
    private static function loadItems($type)
    {
        self::$_items[$type]=array();
        $models=self::model()->findAll(array(
            'condition'=>'type=:type',
            'params'=>array(':type'=>$type),
            'order'=>'position',
        ));
        foreach($models as $model)
            self::$_items[$type][$model->code]=$model->name;
    } // }}} 
    // }}} 
}

/* vim: set ai sw=4 sts=4 et fdm=marker fdc=4 */ 
?>
